package com.thauanLopes.bacterium;

import java.util.ArrayList;
import java.util.List;

import Bacterias.HelmetBacteria;
import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.game.ThauanFramework.GameObject;
import com.game.ThauanFramework.MinhasCoisas;
import com.game.ThauanFramework.TimeManager;
import com.thauanLopes.bacterium.GameManages.CashManager;
import com.thauanLopes.bacterium.GameManages.GameManager;
import com.thauanLopes.bacterium.GameManages.GameManagerFactory;
import com.thauanLopes.bacterium.GameManages.ScoreManager;
import com.thauanLopes.smagasmaga.R;

public class GameSceneView extends View implements Runnable {

	private String TAG = "GameScene";

	private boolean running = true;
	private boolean lost = false;

	private GameManager currentGameManager;
	private GameManagerFactory gameManagerFactory;

	// Thread Interval
	private int interval = 10;
	Thread mainThread;
	
	// To catch all the scene game objects and null them.
	public static List<GameObject> cemetery = new ArrayList<GameObject>();

	public static int canvasHeight;
	public static int canvasWidth;

	public GameSceneView(Context context) {
		super(context);
		running = true;

		ScoreManager.getInstance().Reset();
		
		this.setFocusable(true);
		mainThread = new Thread(this);

	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		canvasWidth = w;
		canvasHeight = h;

		MinhasCoisas.setCanvasWidth(w);
		MinhasCoisas.setCanvasHeight(h);

		ChangeGameMode(GameManagerFactory.MODE_NORMAL);
		
		mainThread.start();

		//this.setBackgroundResource(R.drawable.bacterium_fundo_medium);
	}

	public boolean onTouchEvent(MotionEvent event) {
		
		boolean returnBool = false;
		
		if(currentGameManager != null)
		returnBool = currentGameManager.onTouch(event);
		
	 	return returnBool;

	}

	// ------------------------------------------------------------- START
	public void Start() {
	}

	// ------------------------------------------------------------- UPDATE
	public void Update() {

		if (this.lost) {
			MinhasCoisas.getCurrentActivity().finish();
			this.SetAlive(false);
			if (ScoreManager.getInstance().getScore() > ScoreManager
					.getHighScore()) {
				ScoreManager
						.setHighScore(ScoreManager.getInstance().getScore());
				ScoreManager.getInstance().Reset();
			}
			return;
		}

		// GameManager Update
		if (currentGameManager != null)
			this.currentGameManager
					.Update(TimeManager.getInstance().getDeltaTime());

		// ScoreManager Update
		ScoreManager.getInstance().Update(
				TimeManager.getInstance().getDeltaTime());
	}

	// Draw
	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);

		// ScoreManager Draw
		ScoreManager.getInstance().Draw(canvas);
		CashManager.getInstance().Draw(canvas);
		
		try {
			
			// ============================================================
			// Destroy elements no more used
			if (!cemetery.isEmpty()) {

				for (GameObject gb : cemetery) {
					gb.Destroy();
					gb = null;
				}
			}
			cemetery.clear();
			// ===========================================================
			
			// GameManager Draw
			if (this.currentGameManager != null)
				this.currentGameManager.Draw(canvas);

		} catch (Exception e) {

		}
	}

	public void SetAlive(boolean bool) {
		this.running = bool;
	}

	public boolean isAlive() {
		return this.running;
	}

	@Override
	public void run() {
		this.Start();

		while (running) {
			try {
				Thread.sleep(interval);
			} catch (InterruptedException e) {
				running = false;
				Log.i(TAG, "Erro no processo de Thread", e);
			}

			Update();

			// Reload drawables on the screen
			postInvalidate();
		}

	}

	// ----------------------------- Getters And Setters

	public boolean isLost() {
		return lost;
	}

	public void setLost(boolean lost) {
		this.lost = lost;
	}
	
	public void ChangeGameMode(int gameManagerIndex)
	{
		if(this.gameManagerFactory == null)
			this.gameManagerFactory = new GameManagerFactory(cemetery, this);
		
		this.currentGameManager = this.gameManagerFactory.Create(gameManagerIndex);
	}
}
